/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.fee.core.service;

import info.peigen.hotpot.business.fee.facade.command.FeeServiceFacade;
import info.peigen.hotpot.business.fee.facade.command.order.FeeOrder;
import info.peigen.hotpot.business.fee.facade.command.result.FeeResult;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import org.noear.solon.annotation.Component;

/**
 * <b>(FeeService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/26
 */
@SuppressWarnings("rawtypes")
@Component
public class FeeService extends AbstractFeeService implements FeeServiceFacade {
    @Override
    public FeeResult calculate(FeeOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FeeResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(checkCalculateFeeProcessor, activeCalculateFeeProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(calculateFeeProcessor, feeRuleCompleteProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeFeeLogProcessor, calculateFeeResultProcessor);
            }
        });
        return (FeeResult) serviceContext.getResult();
    }

    @Override
    public FeeResult fee(FeeOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FeeResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(checkCalculateFeeProcessor, activeCalculateFeeProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(calculateFeeProcessor, feeRuleCompleteProcessor, feeAccountTransferProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeFeeLogProcessor, calculateFeeResultProcessor);
            }
        });
        return (FeeResult) serviceContext.getResult();
    }
}